package com.steel.yfmall.cloud.assistant.utils.kd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CosmicLoginUtil {
    @Value("${app_url}")
    public void setAPP_URL(String value) {
        APP_URL = value;
    }
    @Value("${access_url}")
    public void setACCESS_URL(String value) {
        ACCESS_URL = value;
    }
    @Value("${appSecuret}")
    public void setpassword(String value) {
        password = value;
    }
    @Value("${accessType}")
    public void setaccessType(String value) {
        accessType = value;
    }
    @Value("${appType}")
    public void setappType(String value) {
        appType = value;
    }
    @Value("${appId}")
    public void setappId(String value) {
        appId = value;
    }
    @Value("${appSecuret}")
    public void setappSecuret(String value) {
        appSecuret = value;
    }
    @Value("${tenantid}")
    public void settenantid(String value) {
        tenantid = value;
    }
    @Value("${accountid}")
    public void setaccountid(String value) {
        accountid = value;
    }
    @Value("${language}")
    public void setlanguage(String value) {
        language = value;
    }
    @Value("${accessuser}")
    public void setaccessuser(String value) {
        accessuser = value;
    }
    @Value("${userType}")
    public void setuserType(String value) {
        userType = value;
    }



    public static String APP_URL;
    public static String ACCESS_URL;
    public static String password="";
    public static String accessType;
    public static String appType;
    public static String appId;//开放平台第三方API系统编码
    public static String appSecuret;//开放平台第三方API系统密码
    public static String tenantid;//租户中心id
    public static String accountid="";//租户中心数据中心ID
    public static String language;//获取AppToken所用参数
    public static String accessuser;
    public static String userType;

    public static String getAccessToken(String  apptoken){
        String accessToken="";
        try {
            String postparam = JSON.toJSONString(new AccessToken(accessuser, apptoken, tenantid, accountid, userType));
            log.info("获取accessToken所用参数:" + postparam);
            String res = GetHttpMsg(postparam, ACCESS_URL);
            accessToken = getHttpJsonData(accessType, res);
            log.info("获取accessToken结果:" + accessToken);
        }catch (Exception ex){
            log.info("获取accessToken结果失败:" + ex.getMessage());
        }
        return accessToken;
    }
    public static String GetAPPToken(){
        String apptoken="";
        try {
        String postparam=JSON.toJSONString(new AppToken(appId,appSecuret,tenantid,accountid,language));
        log.info("获取appToken所用参数:"+postparam);
        String res=GetHttpMsg(postparam,APP_URL);
        apptoken=getHttpJsonData(appType,res);
        log.info("获取appToken结果:"+apptoken);
        }catch (Exception ex){
            log.info("获取appToken结果失败:" + ex.getMessage());
        }
        return apptoken;
    }
    private static String getHttpJsonData(String loginType,String res) {
        String str="";
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject obj=jsonObject.getJSONObject("data");
        boolean sucess=obj.getBoolean("success");
        if(sucess){
            if(loginType.equals(appType)){
                str=obj.getString("app_token");
            }else if(loginType.equals(accessType)){
                str=obj.getString("access_token");
            }

        }
        return str;
    }
    public static String GetHttpMsg(String str,String url){
        log.info("HttpClient的URL="+url+",参数:"+str);
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;
        StringEntity entity = new StringEntity(str,"utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        try{
            HttpResponse resp = client.execute(httpPost);
            System.out.println(resp.getStatusLine().getStatusCode());
            if(resp.getStatusLine().getStatusCode() == 200) {
                HttpEntity he = resp.getEntity();
                respContent = EntityUtils.toString(he, "UTF-8");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        log.info("HTTPClient请求结果:"+respContent);
        return respContent;
    }

    public static String getHttpMsg(String str,String url){
        log.info("HttpClient的URL="+url);
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;
        StringEntity entity = new StringEntity(str,"utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        try{
            HttpResponse resp = client.execute(httpPost);
            System.out.println(resp.getStatusLine().getStatusCode());
            if(resp.getStatusLine().getStatusCode() == 200) {
                HttpEntity he = resp.getEntity();
                respContent = EntityUtils.toString(he, "UTF-8");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return respContent;
    }

}
